import requests, logging, os, qrcode
from PIL import Image

from equity_mall.utils.qiniu_storage import pic_upload

logger = logging.getLogger('django')


def sync_img_to_local(url, key=None):
    try:
        img_str = requests.get(url).content
        return pic_upload(img_str, key=key)
    except Exception as e:
        logging.error(e)
        return ''


def make_qrcode(shop_id, out_shop_id_url, key):
    qr = qrcode.QRCode(
        version=2,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=10,
        border=1
    )  # 设置二维码的大小
    qr.add_data(out_shop_id_url)
    qr.make(fit=True)
    img = qr.make_image()
    img = img.convert("RGBA")
    img_w, img_h = img.size
    factor = 4.5
    size_w = int(img_w / factor)
    size_h = int(img_h / factor)
    icon = Image.open(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), f'static/20220531164242.png')).convert("RGBA")
    icon_w, icon_h = icon.size
    if icon_w > size_w:
        icon_w = size_w
    if icon_h > size_h:
        icon_h = size_h
    icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
    w = int((img_w - icon_w) / 2)
    h = int((img_h - icon_h) / 2)
    img.paste(icon, (w, h), icon)
    # path = f"./../../../static/{shop_id}.png"
    path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), f'static/{shop_id}.png')
    img.save(path)
    f = open(path, 'rb')
    pic_data = f.read()
    f.close()
    os.remove(path)
    return pic_upload(pic_data, key=key)


def make_qrcode3(shop_id, out_shop_id_url, key):
    qr = qrcode.QRCode(
        version=2,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=10,
        border=1
    )  # 设置二维码的大小
    qr.add_data(out_shop_id_url)
    qr.make(fit=True)
    img = qr.make_image()
    img = img.convert("RGBA")
    img_w, img_h = img.size
    factor = 4.5
    size_w = int(img_w / factor)
    size_h = int(img_h / factor)
    icon = Image.open(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), f'static/20221118162938.png')).convert("RGBA")
    icon_w, icon_h = icon.size
    if icon_w > size_w:
        icon_w = size_w
    if icon_h > size_h:
        icon_h = size_h
    icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
    w = int((img_w - icon_w) / 2)
    h = int((img_h - icon_h) / 2)
    img.paste(icon, (w, h), icon)
    # path = f"./../../../static/{shop_id}.png"
    path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), f'static/{shop_id}.png')
    img.save(path)
    f = open(path, 'rb')
    pic_data = f.read()
    f.close()
    os.remove(path)
    return pic_upload(pic_data, key=key)


def make_qrcode2(shop_id, out_shop_id_url, key):
    qr = qrcode.QRCode(
        version=2,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=10,
        border=1
    )  # 设置二维码的大小
    qr.add_data(out_shop_id_url)
    qr.make(fit=True)
    img = qr.make_image()
    img = img.convert("RGBA")
    img_w, img_h = img.size
    factor = 4.5
    size_w = int(img_w / factor)
    size_h = int(img_h / factor)
    icon = Image.open(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), f'static/20220908143203.png')).convert("RGBA")
    icon_w, icon_h = icon.size
    if icon_w > size_w:
        icon_w = size_w
    if icon_h > size_h:
        icon_h = size_h
    icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
    w = int((img_w - icon_w) / 2)
    h = int((img_h - icon_h) / 2)
    img.paste(icon, (w, h), icon)
    # path = f"./../../../static/{shop_id}.png"
    path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), f'static/{shop_id}.png')
    img.save(path)
    f = open(path, 'rb')
    pic_data = f.read()
    f.close()
    os.remove(path)
    return pic_upload(pic_data, key=key)


def make_qrcode4(shop_id, out_shop_id_url, key):
    qr = qrcode.QRCode(
        version=2,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=10,
        border=1
    )  # 设置二维码的大小
    qr.add_data(out_shop_id_url)
    qr.make(fit=True)
    img = qr.make_image()
    img = img.convert("RGBA")
    img_w, img_h = img.size
    factor = 4.5
    size_w = int(img_w / factor)
    size_h = int(img_h / factor)
    icon = Image.open(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), f'static/20231205091011.png')).convert("RGBA")
    icon_w, icon_h = icon.size
    if icon_w > size_w:
        icon_w = size_w
    if icon_h > size_h:
        icon_h = size_h
    icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
    w = int((img_w - icon_w) / 2)
    h = int((img_h - icon_h) / 2)
    img.paste(icon, (w, h), icon)
    # path = f"./../../../static/{shop_id}.png"
    path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), f'static/{shop_id}.png')
    img.save(path)
    f = open(path, 'rb')
    pic_data = f.read()
    f.close()
    os.remove(path)
    return pic_upload(pic_data, key=key)


def make_qrcode5(shop_id, out_shop_id_url, key):
    qr = qrcode.QRCode(
        version=2,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=10,
        border=1
    )  # 设置二维码的大小
    qr.add_data(out_shop_id_url)
    qr.make(fit=True)
    img = qr.make_image()
    img = img.convert("RGBA")
    img_w, img_h = img.size
    factor = 4.5
    size_w = int(img_w / factor)
    size_h = int(img_h / factor)
    icon = Image.open(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), f'static/699237541.jpg')).convert("RGBA")
    icon_w, icon_h = icon.size
    if icon_w > size_w:
        icon_w = size_w
    if icon_h > size_h:
        icon_h = size_h
    icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
    w = int((img_w - icon_w) / 2)
    h = int((img_h - icon_h) / 2)
    img.paste(icon, (w, h), icon)
    # path = f"./../../../static/{shop_id}.png"
    path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), f'static/{shop_id}.png')
    img.save(path)
    f = open(path, 'rb')
    pic_data = f.read()
    f.close()
    os.remove(path)
    return pic_upload(pic_data, key=key)


def make_qrcode1(shop_id, out_shop_id_url, key):
    qr = qrcode.QRCode(
        version=2,
        error_correction=qrcode.constants.ERROR_CORRECT_H,
        box_size=10,
        border=1
    )  # 设置二维码的大小
    qr.add_data(out_shop_id_url)
    qr.make(fit=True)
    img = qr.make_image()
    img = img.convert("RGBA")
    img_w, img_h = img.size
    factor = 4.5
    # size_w = int(img_w / factor)
    # size_h = int(img_h / factor)
    # icon = Image.open(os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), f'static/20220531164242.png')).convert("RGBA")
    # icon_w, icon_h = icon.size
    # if icon_w > size_w:
    #     icon_w = size_w
    # if icon_h > size_h:
    #     icon_h = size_h
    # icon = icon.resize((icon_w, icon_h), Image.ANTIALIAS)
    # w = int((img_w - icon_w) / 2)
    # h = int((img_h - icon_h) / 2)
    # img.paste(icon, (w, h), icon)
    # path = f"./../../../static/{shop_id}.png"
    path = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), f'static/{shop_id}.png')
    img.save(path)
    f = open(path, 'rb')
    pic_data = f.read()
    f.close()
    os.remove(path)
    return pic_upload(pic_data, key=key)